<!--
  * 订单
  *
  * @Author:    syf
  * @Date:      2025-05-16 10:02:50
  * @Copyright  畅想科技
-->
<template>
    <!---------- 查询表单form begin ----------->
    <a-form class="smart-query-form">


        
        <a-row class="smart-query-form-row" v-privilege="'support:order:query'">
            <a-form-item label="订单编号" class="smart-query-form-item">
                <a-input style="width: 200px" v-model:value="queryForm.orderId" placeholder="订单编号" />
            </a-form-item>
            <a-form-item label="订单类型" name="orderKind" class="smart-query-form-item">
                <SmartEnumSelect enum-name="ORDERS_STATUS_ENUM" v-model:value="queryForm.orderKind" width="160px" />
            </a-form-item>
              <!-- <a-form-item label="订单类型" name="orderKind">
        <a-select v-model:value="form.orderKind" placeholder="请输入订单类型" >
          <a-select-option value="">全部</a-select-option>
          <a-select-option value="0">代取快递</a-select-option>
          <a-select-option value="1">代买物品</a-select-option>
          <a-select-option value="2">代取外卖</a-select-option>
          <a-select-option value="3">代办事</a-select-option>
        </a-select> -->
        <a-form-item label="下单时间" class="smart-query-form-item" style="margin-right: 20px">
        <a-range-picker
          v-model:value="chooseTimeRange"
          @change="changeCreateDate"
          :presets="defaultTimeRanges"
          format="YYYY-MM-DD"
          style="width: 240px"
        />
      </a-form-item>
        <a-form-item class="smart-query-form-item">
                <a-button type="primary" @click="queryData">
                    <template #icon>
                        <SearchOutlined />
                    </template>
                    查询
                </a-button>
                <a-button @click="resetQuery" class="smart-margin-left10">
                    <template #icon>
                        <ReloadOutlined />
                    </template>
                    重置
                </a-button>
            </a-form-item>
        </a-row>
    </a-form>
    <!---------- 查询表单form end ----------->

    <a-card size="small" :bordered="false" :hoverable="true">
        <!---------- 表格操作行 begin ----------->
        <a-row class="smart-table-btn-block">
            <div class="smart-table-operate-block">
                <a-button @click="showForm" type="primary" size="small">
                    <template #icon>
                        <PlusOutlined />
                    </template>
                    新建
                </a-button>
                <a-button @click="confirmBatchDelete" type="danger" size="small" :disabled="selectedRowKeyList.length == 0">
                    <template #icon>
                        <DeleteOutlined />
                    </template>
                    批量删除
                </a-button>
            </div>
            <div class="smart-table-setting-block">
                <TableOperator v-model="columns" :tableId="null" :refresh="queryData" />
            </div>
        </a-row>
        <!---------- 表格操作行 end ----------->

        <!---------- 表格 begin ----------->
        <a-table
                size="small"
                :dataSource="tableData"
                :columns="columns"
                rowKey="orderId"
                bordered
                :loading="tableLoading"
                :pagination="false"
                :row-selection="{ selectedRowKeys: selectedRowKeyList, onChange: onSelectChange }"
        >
            <template #bodyCell="{ column, text, record, index }">
                <span v-if="column.dataIndex === 'serialNumber'">{{ index + 1 }}</span>
                <template v-if="column.dataIndex === 'action'">
                    <div class="smart-table-operate">
                        <a-button @click="showForm(record)" type="link">编辑</a-button>
                        <a-button @click="showDetail(record.orderId)" type="link" v-privilege="'support:order:query'">详情</a-button>
                        <!-- <a-button @click="onDelete(record)" danger type="link">删除</a-button> -->
                    </div>
                </template>
                <template v-if="column.dataIndex === 'orderStateName'">
                  <div v-if="record.orderStateName==='已完成'">                    
                        <a-tag color="success">
                            <template #icon >
                                <check-circle-outlined />
                            </template>
                                {{ record.orderStateName }}
                        </a-tag>
                    </div>

                    <div v-if="record.orderStateName!=='已完成'&record.orderStateName!=='已取消'">
                        <a-tag color="warning">
                        <template #icon >
                               <sync-outlined :spin="true" />
                        </template>
                            {{ record.orderStateName }}
                    </a-tag>
                    </div>
                        <div v-if="record.orderStateName==='已取消'">
                        <a-tag color="error">
                        <template #icon >
                              <close-circle-outlined />
                        </template>
                            {{ record.orderStateName }}
                        </a-tag>
                    </div>
                </template>
                <template v-if="column.dataIndex === 'orderKind'">
                    <span>{{ $smartEnumPlugin.getDescByValue('ORDERS_STATUS_ENUM', text) }}</span>
                </template>

            </template>
        </a-table>
        <!---------- 表格 end ----------->

        <div class="smart-query-table-page">
            <a-pagination
                    showSizeChanger
                    showQuickJumper
                    show-less-items
                    :pageSizeOptions="PAGE_SIZE_OPTIONS"
                    :defaultPageSize="queryForm.pageSize"
                    v-model:current="queryForm.pageNum"
                    v-model:pageSize="queryForm.pageSize"
                    :total="total"
                    @change="queryData"
                    @showSizeChange="queryData"
                    :show-total="(total) => `共${total}条`"
            />
        </div>

        <OrderForm  ref="formRef" @reloadList="queryData"/>
        <OrderDetailModal ref="detailModal" />
    </a-card>
</template>
<script setup>
    import { reactive, ref, onMounted } from 'vue';
    import { message, Modal } from 'ant-design-vue';
    import OrderDetailModal from './order-detail-modal.vue';
    import { SmartLoading } from '/@/components/framework/smart-loading';
    import { orderApi } from '/@/api/business/order/order-api';
    import { PAGE_SIZE_OPTIONS } from '/@/constants/common-const';
    import { ORDERS_STATUS_ENUM } from '/@/constants/support/orders-const';
    import { smartSentry } from '/@/lib/smart-sentry';
    import TableOperator from '/@/components/support/table-operator/index.vue';
    import SmartEnumSelect from '/@/components/framework/smart-enum-select/index.vue';
    import OrderForm from './order-form.vue';
    // ---------------------------- 表格列 ----------------------------

    const columns = ref([
        {
          title: '序号',
          dataIndex: 'serialNumber', // 这个dataIndex仅作为标识，不对应实际数据字段
          width: '80px', // 可以自定义宽度
          scopedSlots: { customRender: 'bodyCell' }, // 对应模板中的具名插槽
          align:"center"
        },
        {
            title: '订单编号',
            dataIndex: 'orderId',
            width:175,
            ellipsis: true,
            align:"center"

        },
        {
            title: '订单类型',
            dataIndex: 'orderKind',
            width:100,
            ellipsis: true,
            align:"center"

        },
        {
            title: '取货地址',
            dataIndex: 'orderStartName',
            width:200,
            ellipsis: true,
            align:"center"
        },
        {
            title: '配送地址',
            dataIndex: 'orderEndName',
            width:200,
            ellipsis: true,
            align:"center"
        },
        // {
        //     title: '订单创建时间',
        //     dataIndex: 'createTime',
        //     ellipsis: true,
        // },
        {
            title: '下单时间',
            dataIndex: 'orderOpen',
            ellipsis: true,
            align:"center"
        },
        {
            title: '截止时间',
            dataIndex: 'deadTime',
            ellipsis: true,
            align:"center"
        },
        {
            title: '备注',
            dataIndex: 'reMark',
            ellipsis: true,
            align:"center"
        },

        {
            title: '下单人',
            dataIndex: 'userName',
            ellipsis: true,
            align:"center"
        },
        // {
        //     title: '审核人员名称',
        //     dataIndex: 'shBy',
        //     ellipsis: true,
        //     align:"center"
        // },
        // {
        //     title: '审核时间',
        //     dataIndex: 'shTime',
        //     ellipsis: true,
        //     align:"center"
        // },
        // {
        //     title: '接单员信息',
        //     dataIndex: 'jdyId',
        //     ellipsis: true,
        // },
        // {
        //     title: '接单员接单时间',
        //     dataIndex: 'jdyTime',
        //     ellipsis: true,
        // },
        // {
        //     title: '订单完成时间',
        //     dataIndex: 'competeTime',
        //     ellipsis: true,
        // },
        // {
        //     title: '信息更改人员',
        //     dataIndex: 'updateBy',
        //     ellipsis: true,
        //     align:"center"
        // },
        // {
        //     title: '信息修改时间',
        //     dataIndex: 'updateTime',
        //     ellipsis: true,
        //     align:"center"
        // },
        {
            title: '订单状态',
            dataIndex: 'orderStateName',
            ellipsis: true,
            align:"center"
        },
        {
            title: '操作',
            dataIndex: 'action',
            fixed: 'right',
            width: 90,
            align:"center"
        },
    ]);

    // ---------------------------- 查询数据表单和方法 ----------------------------

    const queryFormState = {
        orderId:'',
        orderKind:ORDERS_STATUS_ENUM.ALL.value,
        startDate: undefined,
        endDate:undefined,
        pageNum: 1,
        pageSize: 10,
    };
    // 查询表单form
    const queryForm = reactive({ ...queryFormState });
    // 表格加载loading
    const tableLoading = ref(false);
    // 表格数据
    const tableData = ref([]);
    // 总数
    const total = ref(0);
  const chooseTimeRange = ref([]);

  function changeCreateDate(value, dateString) {
    queryForm.startDate = dateString[0];
    queryForm.endDate = dateString[1];
  }
    // 重置查询条件
    function resetQuery() {
        let pageSize = queryForm.pageSize;
        Object.assign(queryForm, queryFormState);
        queryForm.pageSize = pageSize;
        chooseTimeRange.value = [];
        queryData();
    }

    // 查询数据
    async function queryData() {
        tableLoading.value = true;
        try {
            let queryResult = await orderApi.queryPage(queryForm);
            tableData.value = queryResult.data.list;
            total.value = queryResult.data.total;
        } catch (e) {
            smartSentry.captureError(e);
        } finally {
            tableLoading.value = false;
        }
    }

  const typeList = ref([]); //订单状态列表
  async function queryAll() {
    let res = await orderApi.queryType();
    typeList.value = res.data;
  }
    onMounted(queryData);

    // ---------------------------- 添加/修改 ----------------------------
    const formRef = ref();

    function showForm(data) {
        formRef.value.show(data);
    }

    // ---------------------------- 单个删除 ----------------------------
    //确认删除
    function onDelete(data){
        Modal.confirm({
            title: '提示',
            content: '确定要删除选吗?',
            okText: '删除',
            okType: 'danger',
            onOk() {
                requestDelete(data);
            },
            cancelText: '取消',
            onCancel() {},
        });
    }

    //请求删除
    async function requestDelete(data){
        SmartLoading.show();
        try {
            let deleteForm = {
                goodsIdList: selectedRowKeyList.value,
            };
            await orderApi.delete(data.orderId);
            message.success('删除成功');
            queryData();
        } catch (e) {
            smartSentry.captureError(e);
        } finally {
            SmartLoading.hide();
        }
    }

    // ---------------------------- 批量删除 ----------------------------

    // 选择表格行
    const selectedRowKeyList = ref([]);

    function onSelectChange(selectedRowKeys) {
        selectedRowKeyList.value = selectedRowKeys;
    }

    // 批量删除
    function confirmBatchDelete() {
        Modal.confirm({
            title: '提示',
            content: '确定要批量删除这些数据吗?',
            okText: '删除',
            okType: 'danger',
            onOk() {
                requestBatchDelete();
            },
            cancelText: '取消',
            onCancel() {},
        });
    }

    //请求批量删除
    async function requestBatchDelete() {
        try {
            SmartLoading.show();
            await orderApi.batchDelete(selectedRowKeyList.value);
            message.success('删除成功');
            queryData();
        } catch (e) {
            smartSentry.captureError(e);
        } finally {
            SmartLoading.hide();
        }
    }
    const detailModal = ref();
    function showDetail(orderId) {
    detailModal.value.show(orderId);
  }
</script>
